local fengji = fk.CreateSkill {
  name = "th_fengji",
  tags = { Skill.Compulsory },
  attached_skill_name = "th_fengji&",
  max_phase_use_time = 1,
}

fengji:addEffect("active", {
  anim_type = "support",
  handly_pile = true,
  card_num = function(self, player)
    return player:getMark("@th_fengji") + 1
  end,
  min_target_num = 1,
  prompt = function(self, player, cards, targets)
    return "#th_fengji:::" .. player:getMark("@th_fengji") + 1
  end,
  can_use = function(self, player)
    local card = Fk:cloneCard("amazing_grace")
    return not player:isKongcheng() and player:canUse(card) and not player:prohibitUse(card)
  end,
  card_filter = function(self, player, to_select, selected)
    return #selected < player:getMark("@th_fengji") + 1 and Fk:getCardById(to_select).color == Card.Red and Fk:currentRoom():getCardArea(to_select) == Player.Hand
  end,
  target_filter = function(self, player, to_select, selected, cards)
    local card = Fk:cloneCard("amazing_grace")
    card:addSubcards(cards)
    return #cards >= self:getMinCardNum(player) and (table.contains(selected, player) or to_select == player) and player:canUseTo(card, to_select)
  end,
  feasible = function(self, player, selected, selected_cards)
    local card = Fk:cloneCard("amazing_grace")
    card.skillName = fengji.name
    return table.contains(selected, player) and #selected >= self:getMinTargetNum(player) and #selected <= card.skill:getMaxTargetNum(player, card)
    and #selected_cards >= self:getMinCardNum(player) and #selected_cards <= self:getMaxCardNum(player)
  end,
  on_use = function(self, room, effect)
    local card = Fk:cloneCard("amazing_grace")
    room:addPlayerMark(effect.from, "@th_fengji", 1)
    card:addSubcards(effect.cards)
    card.skillName = fengji.name
    room:useCard{
      from = effect.from,
      tos = effect.tos,
      card = card,
    }
  end,
}, { check_skill_limit = true })
fengji:addEffect(fk.TurnEnd, {
  is_delay_effect = true,
  late_refresh = true,
  mute = true,
  can_refresh = function(self, event, target, player, data)
    return player:hasSkill("th_fengji", true) and target == player
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "@th_fengji", 0)
  end,
})

Fk:loadTranslationTable{
  ["th_fengji"] = "丰祭",
  [":th_fengji"] = "每名角色出牌阶段限一次，其可以交给你一张红色手牌，然后你可以将X张红色手牌当【五谷丰登】对包括其的任意名角色使用（X为你的上个回合结束后此技能发动的次数）。",

  ["#th_fengji"] = "丰祭：将%arg张红色手牌当【五谷丰登】对包括你的任意名角色使用",

  ["@th_fengji"] = "丰祭",
}

return fengji